<?php

namespace App\Admin\Controllers\Voter;

use App\Admin\Actions\Grid\Voter\RefusePlayerNoticeAction;
use App\Admin\Repositories\Voter\WxVoterPlayer;
use App\Models\Voter\WxVoter;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxVoterPlayerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxVoterPlayer(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('owers', '头像')->display(function ($v) {
                return '<img src="' . ($this->ower['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('ower', '用户名称')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');
            $grid->column('voter_id');
            $grid->column('relative_id');
            $grid->column('show_name');
            $grid->column('work_name');
//            $grid->column('work_desc');
            $grid->column('img_urls')->display(function ($v){
                $str = '';
                if($v){
                    foreach ($v as $img){
                        $str .= '<a href="'.$img.'" target="_blank"><div style="background-image: url(\''.$img.'\');background-size: cover; width: 87px; height: 100px;"/></div></a>';
                        break;
                    }
                }
                return $str;
            });

            $grid->column('video_url')->display(function ($v){
                if($v){
                    return '<video controls width="150">
  <source src="'.$v.'" type="video/mp4">
  您的浏览器不支持 video 标签。
</video>';
                }
                return '';
            });
            $grid->column('video_cover')->image();
            $grid->column('contact_text');
            $grid->column('status')->switch();
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('voter_id');
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append((new RefusePlayerNoticeAction())->setKey($this->id));
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxVoterPlayer(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('voter_id');
            $show->field('relative_id');
            $show->field('show_name');
            $show->field('work_name');
            $show->field('work_desc');
            $show->field('img_urls');
            $show->field('video_url');
            $show->field('video_cover');
            $show->field('contact_text');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxVoterPlayer(), function (Form $form) {
            $form->display('id');

            $form->select('user_id', '用户')->options(function ($id) {
                $user = \App\Models\User\WxUser::find($id);
                if ($user) {
                    return [$user->id => $user->user_name];
                }
            })->ajax('select/users');

            $form->select('voter_id')->options(function ($id) {
                $user = WxVoter::find($id);
                if ($user) {
                    return [$user->id => $user->title];
                }
            })->ajax('select/voters');

            $form->text('relative_id', '编号')->help('不能重复，建议从1开始加');
            $form->text('show_name');
            $form->text('work_name');
            $form->textarea('work_desc');
            $form->multipleImage('img_urls')->limit(100)->url('files/uploads')->uniqueName()->autoUpload()->saveAsJson();
            $form->text('video_url');
            $form->text('video_cover');
            $form->text('contact_text');
            $form->switch('status')->default(1);

            $form->display('created_at');
            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
